﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using H2.NET.Business;
using H2.NET.Data.Common;

namespace H2.NET.Web.Pages
{
    public partial class TabMaterial : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        public string MMActions
        {
            get
            {
                return ViewState["MMActions"] == null ? "" : (string)ViewState["MMActions"]; 
            }
            set { ViewState["MMActions"] = value; }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public string SAP_Code
        {
            get
            {
                return this.SAPCode1.SAP_SAP_Code.Text;

            }
        }

        public string SubLine
        {
            get
            {
                return this.Technical1.TEC_Subline.Text;

            }
        }

        public string Density
        {
            get
            {
                return this.Technical1.TEC_Density.Text;
            }
        }

        public string Color_Code
        {
            get
            {
                return this.Technical1.TEC_Color_Code.Text;
            }

        }

        public string Special_Procurement
        {
            get
            {
                return this.Purchasing1.PUR_Special_Procurement.Text;
            }

        }

        public string Stock_Det_Grp
        {
            get
            {
                return this.Planning1.PLA_Stock_Det_Grp.Text;
            }

        }

        public string Volume_Rebate_Group
        {
            get
            {
                return this.Marketing1.MKT_Volume_Rebate_Group.SelectedValue;
            }

        }

        public string Description_EN
        {
            get
            {
                return this.Marketing1.MKT_Description_EN.Text;
            }

        }

        public bool IsInspectionSetup
        {
            get
            {
                if(!Status.IsUsed)
                {
                    return false;
                }

                //
                foreach (mdc_Material mm in Materials)
                {
                    if (mm != null && !string.IsNullOrEmpty(mm.TEC_Inspection_Setup) 
                                  && mm.TEC_Inspection_Setup == "Y")
                    {
                        return true;
                    }
                }

                return false;
            }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

        public List<mdc_Material> Materials
        {
            get
            {
                if (StoreAPI["TabValue~" + Key] == null)
                {
                    StoreAPI["TabValue~" + Key] = new List<mdc_Material>();
                }

                return (List<mdc_Material>)StoreAPI["TabValue~" + Key];
            }
            set { StoreAPI["TabValue~" + Key] = value; }
        }

        private bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }

        private bool IsShowSAPCode
        {
            get
            {
                return ViewState["IsShowSAPCode"] == null ? false : (bool)ViewState["IsShowSAPCode"];
            }
            set { ViewState["IsShowSAPCode"] = value; }
        }

        #endregion

        #region 事件 页面加载
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        #endregion

        #region 事件 Sheets 选择
        private void SetAD()
        {
            if (TaskAPI.Process=="MTO Template Data Collection"||TaskAPI.Process == "NPL Master Data Application" || TaskAPI.Process == "TW NPL Application-DECO" || TaskAPI.Process == "TW NPL Application-PKG")
            {
                switch (TaskAPI.Step)
                {
                    case "Fill in the request":
                    case "Technical":
                    case "SH Planning":
                    case "GZ Planning":
                    case "LF Planning":
                    case "Purchasing":
                    case "Planning":
                    case "PKG Purchasing":
                    case "PKG Planning":
                    case "PKG Logistic":
                    case "Logistic":
                    case "RDC Plant Extend":
                    case "OEM Planning":
                        Add.Visible = true;
                        if (Materials[Sheets.SelectedIndex < 0 ? 0 : Sheets.SelectedIndex].SheetStep != TaskAPI.Step)
                        {
                            Delete.Visible = false;
                        }
                        else
                        {
                            Delete.Visible = true;
                        }
                        if (Sheets.SelectedItem.Text.Length > 2)
                        { Add.Visible = false; }
                        else
                        { Add.Visible = true; }


                        break;

                    //if (Sheets.Text.Length > 2)
                    //{
                    //    if (Sheets.Text.IndexOf(TaskAPI.Step.Substring(0, 2)) > 0)
                    //    {
                    //        Add.Visible = false;
                    //        Delete.Visible = true;
                    //    }
                    //    else
                    //    {
                    //        Add.Visible = true;
                    //        Delete.Visible = false;
                    //    }
                    //}
                    //else
                    //{
                    //    Add.Visible = false;
                    //    Delete.Visible = false;
                    //}
                    //break;
                    default:
                        Add.Visible = false;
                        Delete.Visible = false;
                        break;
                }
            }
            else
            {

            }
        }
        protected void Sheets_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TaskAPI.Step.IndexOf("Planning") >= 0 && (TaskAPI.Process == "NPL Master Data Application" || TaskAPI.Process == "MTO Template Data Collection"))
                Planning1.SheetStep = Materials[Sheets.SelectedIndex].SheetStep;
            if (SheetIndex != -1)
            {
                Collect(SheetIndex);
            }

            if (Sheets.SelectedIndex != -1)
            {
                //SetVisible(Materials[Sheets.SelectedIndex].Step == TaskAPI.Step, Delete);
                Distribute(Sheets.SelectedIndex);
                SheetIndex = Sheets.SelectedIndex; 
                //Planning1.PLA_Plant_SelectedIndexChanged(null, e);
            }
            SetAD();
            InitSheets();
            
        }
        #endregion

        #region 事件 Sheets 增加
        protected void Add_Click(object sender, ImageClickEventArgs e)
        {
            if (TaskAPI.Process == "MTO Template Data Collection" || TaskAPI.Process == "NPL Master Data Application" || TaskAPI.Process == "TW NPL Application-DECO" || TaskAPI.Process == "TW NPL Application-PKG")
            {
                string[,] si = new string[Sheets.Items.Count + 1, 2];
                int j = 0, k = Sheets.SelectedIndex;
                int iMax = 0;
                for(int i = 0;i< Sheets.Items .Count; i++)
                {
                    string[] t = Sheets.Items[i].Text.Split('.') ;
                    iMax =  Convert.ToInt16(t[0]) > iMax ? Convert.ToInt16(t[0]) : iMax;
                }
                iMax++;
                for (int i = 0; i <= si.GetUpperBound(0); i++)
                {
                    if (i != Sheets.SelectedIndex)
                    { si[i, 0] = Sheets.Items[j].Text; si[i, 1] = Sheets.Items[j].Value; j++; }
                    else
                    { si[i, 0] = TaskAPI.Step.IndexOf("Planning") >= 0 && TaskAPI.Step.Length > 2 ? iMax.ToString() + "." + TaskAPI.Step.Substring(0, 2) + "<" + Sheets.SelectedValue + ">" : iMax.ToString(); si[i, 1] = iMax.ToString(); }
                }
                Sheets.Items.Clear();
                for (int i = 0; i <= si.GetUpperBound(0); i++)
                {
                    Sheets.Items.Add(new ListItem(si[i, 0], si[i, 1]));
                }
                Sheets.SelectedIndex = k;
                Planning1.SheetStep = TaskAPI.Step;
                if (TaskAPI.Process == "NPL Master Data Application")
                {
                    if (TaskAPI.Step.IndexOf("Planning") >= 0)
                    {
                        SetForeColor(System.Drawing.Color.Blue, Planning1.PLA_Plant_L);
                        Planning1.SetNPLPlant(TaskAPI.Step);
                    }
                }
            }
            else
            { 
                Sheets.Items.Add(new ListItem((Sheets.Items.Count + 1).ToString(), (Sheets.Items.Count + 1).ToString())); 
            }

            //Sheets.Items.Add(new ListItem(TaskAPI.Step.IndexOf("Planning") >= 0 && TaskAPI.Step.Length > 2 ? (Sheets.Items.Count + 1).ToString() + "." + TaskAPI.Step.Substring(0, 2) + "<" + Sheets.SelectedValue + ">" : (Sheets.Items.Count + 1).ToString(), (Sheets.Items.Count + 1).ToString()));
            //Sheets.Items.Add(new ListItem(Sheets.Items[SheetIndex].Text, (Sheets.Items.Count + 1).ToString()));
            mdc_Material mm = new mdc_Material();

            Collect(SheetIndex);
            mm = CopyFrom(Materials[SheetIndex]);
            mm.Step         = TaskAPI.Step;
            mm.SheetStep    = TaskAPI.Step;
            mm.SheetFrom    = SheetIndex.ToString();
            mm.Status       = 1;
            if (TaskAPI.Process == "ADVE Code Application")
            {
                mm.MKT_SKU_BPCS_Code = "";
                mm.MKT_Description_EN = "";
                mm.MKT_Description_ZH = "";
                mm.MKT_Min_dely_qty = "";
                mm.MKT_Min_order_qty = "";
                mm.MKT_Delivery_unit = "";
                mm.MKT_Delivering_plant = "";
                mm.MKT_Order_unit = "";
                //mm.MKT_X_Plant_Matl = "";
                mm.MKT_Base_Unit_Of_Measure = "";
                mm.MKT_Easy_Order_Entry_Code = "";

                mm.LOG_Gross_Weight = "";
                mm.LOG_Net_Weight = "";
                mm.LOG_Volume = "";
                mm.LOG_Quantity_per_box = "";
                //mm.LOG_Unit_Of_Measure_X_1 = "";
                mm.LOG_Unit_Of_Measure_X_2 = "";
                //mm.LOG_Unit_Of_Measure_Y_1 = "";
                mm.LOG_Unit_Of_Measure_Y_2 = "";
                mm.PLA_GR_Processing_Time = "";
                Materials.Insert(Sheets.Items.Count - 1, mm);
            }
            else
            {
                Materials.Insert(SheetIndex, mm);
            }
            //RefreshSheetNo();

            SheetsCount.Text = Sheets.Items.Count.ToString();
            SetAD();
        }
        #endregion

        #region 事件 Sheets 删除
        protected void Delete_Click(object sender, ImageClickEventArgs e)
        {
            if (Sheets.SelectedIndex != -1)
            {
                //至少保留 1 个 Sheet
                if (Sheets.Items.Count == 1)
                {
                    Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                    return;
                }

                if(!string.IsNullOrEmpty(Materials[Sheets.SelectedIndex].ID))
                {
                    Materials[Sheets.SelectedIndex].Status = 0;
                }
                Materials.RemoveAt(Sheets.SelectedIndex);

                if (TaskAPI.Process=="MTO Template Data Collection"||TaskAPI.Process == "NPL Master Data Application" || TaskAPI.Process == "TW NPL Application-DECO" || TaskAPI.Process == "TW NPL Application-PKG")
                {
                    string[,] si = new string[Sheets.Items.Count - 1, 2];
                    int j = 0, k = Sheets.SelectedIndex;
                    for (int i = 0; i < Sheets.Items.Count; i++)
                    {
                        if (i != Sheets.SelectedIndex)
                        { si[j, 0] = Sheets.Items[i].Text; si[j, 1] = Sheets.Items[i].Value; j++; }
                    }
                    Sheets.Items.Clear();
                    for (int i = 0; i <= si.GetUpperBound(0); i++)
                    {
                        Sheets.Items.Add(new ListItem(si[i, 0], si[i, 1]));
                    }
                    Planning1.SheetStep = Materials[Sheets.SelectedIndex].SheetStep;
                }
                else
                {
                    Sheets.Items.Clear();
                    for (int i = 0; i < Materials.Count; i++)
                    {
                        Sheets.Items.Add(new ListItem((i + 1).ToString(), (i + 1).ToString()));
                    }
                }
                Sheets.SelectedIndex = 0;
                SheetIndex           = 0;
                Distribute(SheetIndex);

                //RefreshSheetNo();

                SheetsCount.Text = Sheets.Items.Count.ToString();
                SetAD();
            }
        }
        #endregion

        #region 方法 InitControls(初始化)
        public void InitControls()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!Status.IsUsed)
            {
                return;
            }

            InitShowHide();

            if (Materials.Count == 0)
            {
                Sheets.Items.Add(new ListItem("1", "1"));
                Materials.Add(new mdc_Material());

                SheetsCount.Text = Sheets.Items.Count.ToString();
            }

            mdc_Material mt = Materials[SheetIndex == -1 ? 0 : SheetIndex];
            SAPCode1.InitControls(mt);
            Technical1.InitControls(mt);
            Marketing1.InitControls(mt);
            Planning1.InitControls(mt);
            Purchasing1.InitControls(mt);
            CA1.InitControls(mt);
            Logistics1.InitControls(mt);
            Finance1.InitControls(mt);
            Attachments1.InitControls(mt);
            DMO1.InitControls(mt);
            SetAD();
        }

        private void InitSheets()
        {
            
        }
        #endregion

        #region 方法 Distribute、Collect
        public void Distribute(int Index)
        {
            if (Index == -1 || Materials.Count == 0)
            {
                return;
            }

            mdc_Material mt = Materials[Index];
            SAPCode1.Distribute(mt);
            Technical1.Distribute(mt);
            Marketing1.Distribute(mt);
            Planning1.Distribute(mt);
            Purchasing1.Distribute(mt);
            CA1.Distribute(mt);
            Logistics1.Distribute(mt);
            Finance1.Distribute(mt);
            DMO1.Distribute(mt);
        }

        public void Collect(int Index)
        {
            if (Index == -1 || Materials.Count == 0)
            {
                return;
            }

            mdc_Material mt = Materials[Index];
            SAPCode1.Collect(ref mt);
            Technical1.Collect(ref mt);
            Marketing1.Collect(ref mt);
            Planning1.Collect(ref mt);
            Purchasing1.Collect(ref mt);
            CA1.Collect(ref mt);
            Logistics1.Collect(ref mt);
            Finance1.Collect(ref mt);
            DMO1.Collect(ref mt);
           
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            Collect(SheetIndex);

            //将数据库里当前流程对应的 mdc_Ins_Plan_1 状态置为逻辑删除
            (mdc_Material.process == TaskAPI.Process & mdc_Material.incident == TaskAPI.Incident & mdc_Material.keyword == Key).Edit<mdc_Material>(p =>
            {
                p.Status = 0;
            }
            );

            int i = 1;
            foreach (mdc_Material mm in Materials)
            {
                if (TaskAPI.Process == "MTO Template Data Collection" || TaskAPI.Process == "NPL Master Data Application" || TaskAPI.Process == "TW NPL Application-DECO" || TaskAPI.Process == "TW NPL Application-PKG")
                {
                    mm.Process = TaskAPI.Process;
                    mm.Incident = TaskAPI.Incident;
                    mm.Step = TaskAPI.Step;
                    mm.UserName = UserAPI.UserName;
                    mm.KeyWord = Key;
                    string[] t ;
                    string[] t1 ;
                    if (i - 1 < Sheets.Items.Count)
                    {
                        t = Sheets.Items[i - 1].Text.Split('.');
                        t1 = Sheets.Items[i - 1].Text.Replace(">", "").Split('<');
                    }
                    else 
                    {
                        t = "".Split('.');
                        t1 = "".Split('.');
                    }

                    mm.SheetNo = t[0];
                    mm.SheetFrom = t1.GetUpperBound(0) > 0 ? t1[1] : "";
                    mm.SheetStep = mm.SheetStep == null || mm.SheetStep == "" ? TaskAPI.Step : mm.SheetStep;
                    mm.Time = mm.Time.Year <= 1 ? System.DateTime.Now : mm.Time;
                    mm.Status = 1;
                }
                else
                {

                    mm.Process = TaskAPI.Process;
                    mm.Incident = TaskAPI.Incident;
                    mm.Step = TaskAPI.Step;
                    mm.UserName = UserAPI.UserName;
                    mm.KeyWord = Key;
                    mm.SheetNo = i.ToString();
                    mm.Time = mm.Time.Year <= 1 ? System.DateTime.Now : mm.Time;
                    mm.Status = 1;
                }
                Storage.Save(mm);
                 
                i++;
            }

            Attachments1.Save();
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            if (!Status.IsEdited)
            {
                return new List<string>();
            }

            Collect(SheetIndex);
            List<string> vald_s = new List<string>();
            for (int i = 0; i < Materials.Count; i++ )
            {
                if (TaskAPI.Process == "NPL Master Data Application" || TaskAPI.Process == "TW NPL Application-DECO" || TaskAPI.Process == "TW NPL Application-PKG")
                {
                    if(Sheets.Items.Count > i){
                    string[] tmp = Sheets.Items[i].Text.Split('.');
                    vald_s.AddRange(SAPCode1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + tmp[0] + " => "));
                    vald_s.AddRange(Technical1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + tmp[0] + " => "));
                    vald_s.AddRange(Marketing1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + tmp[0] + " => "));
                    vald_s.AddRange(Planning1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + tmp[0] + " => "));
                    vald_s.AddRange(Purchasing1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + tmp[0] + " => "));
                    vald_s.AddRange(CA1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + tmp[0] + " => "));
                    vald_s.AddRange(Logistics1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + tmp[0] + " => "));
                    vald_s.AddRange(Finance1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + tmp[0] + " => "));
                    vald_s.AddRange(DMO1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + tmp[0] + " => "));
                    }
                }
                else
                {
                    vald_s.AddRange(SAPCode1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + (i + 1).ToString() + " => "));
                    vald_s.AddRange(Technical1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + (i + 1).ToString() + " => "));
                    vald_s.AddRange(Marketing1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + (i + 1).ToString() + " => "));
                    vald_s.AddRange(Planning1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + (i + 1).ToString() + " => "));
                    vald_s.AddRange(Purchasing1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + (i + 1).ToString() + " => "));
                    vald_s.AddRange(CA1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + (i + 1).ToString() + " => "));
                    vald_s.AddRange(Logistics1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + (i + 1).ToString() + " => "));
                    vald_s.AddRange(Finance1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + (i + 1).ToString() + " => "));
                    vald_s.AddRange(DMO1.DataValidation(Materials[i], "&nbsp;&nbsp;S" + (i + 1).ToString() + " => "));
                }
                if (vald_s.Count > 0)
                {
                    vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                    vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
                }
            }

            return vald_s;
        }
        #endregion

        #region 方法 Sheets 复制
        private mdc_Material CopyFrom(mdc_Material mt)
        {
            //mdc_Material _mt = mt.Clone();
            //return _mt;

            mdc_Material _mt = new mdc_Material();
            _mt.KeyWord = mt.KeyWord;
            _mt.SAP_Code = mt.SAP_Code;
            _mt.Status = mt.Status;
            _mt.Step = mt.Step;
            _mt.SheetStep = TaskAPI.Step;
            _mt.SheetFrom = mt.SheetNo;
            _mt.MKT_SKU_BPCS_Code = mt.MKT_SKU_BPCS_Code;
            _mt.MKT_Sales_Org = mt.MKT_Sales_Org;
            _mt.MKT_Distr_Channel = mt.MKT_Distr_Channel;
            _mt.MKT_Volume_Rebate_Group = mt.MKT_Volume_Rebate_Group;
            _mt.MKT_Material_Group_4 = mt.MKT_Material_Group_4;
            _mt.MKT_Product_Hierarchy = mt.MKT_Product_Hierarchy;
            _mt.MKT_Profit_Center = mt.MKT_Profit_Center;
            _mt.MKT_Product_Line_Code = mt.MKT_Product_Line_Code;
            _mt.MKT_Product_Group = mt.MKT_Product_Group;
            _mt.MKT_IsSale = mt.MKT_IsSale;
            _mt.MKT_Base_Unit_Of_Measure = mt.MKT_Base_Unit_Of_Measure;
            _mt.MKT_Division = mt.MKT_Division;
            _mt.MKT_Description_EN = mt.MKT_Description_EN;
            _mt.MKT_Description_ZH = mt.MKT_Description_ZH;
            _mt.MKT_Lab_Office = mt.MKT_Lab_Office;
            _mt.MKT_Easy_Order_Entry_Code = mt.MKT_Easy_Order_Entry_Code;
            _mt.MKT_Marterial_Group = mt.MKT_Marterial_Group;
            _mt.MKT_GenItemCatGroup = mt.MKT_GenItemCatGroup;
            _mt.MKT_X_Plant_Matl = mt.MKT_X_Plant_Matl;
            
            _mt.MKT_Description_EN_1 = mt.MKT_Description_EN_1;
            _mt.MKT_Description_EN_2 = mt.MKT_Description_EN_2;
            _mt.MKT_Description_EN_3 = mt.MKT_Description_EN_3;
            _mt.MKT_Description_EN_4 = mt.MKT_Description_EN_4;
            _mt.MKT_Description_ZH_1 = mt.MKT_Description_ZH_1;
            _mt.MKT_Description_ZH_2 = mt.MKT_Description_ZH_2;
            _mt.MKT_Description_ZH_3 = mt.MKT_Description_ZH_3;
            _mt.MKT_Description_ZH_4 = mt.MKT_Description_ZH_4;
            _mt.MKT_Size_dimension   = mt.MKT_Size_dimension;
            _mt.MKT_FINI_Code        = mt.MKT_FINI_Code;
            _mt.MKT_Is_Generic       = mt.MKT_Is_Generic;
            _mt.MKT_Type_Select      = mt.MKT_Type_Select;
            _mt.MKT_Plant            = mt.MKT_Plant;
            _mt.TEC_Plant = mt.TEC_Plant;
            _mt.TEC_A_Coat = mt.TEC_A_Coat;
            _mt.TEC_Description_EN = mt.TEC_Description_EN;
            _mt.TEC_Description_ZH = mt.TEC_Description_ZH;
            _mt.TEC_Lab_Office = mt.TEC_Lab_Office;
            _mt.TEC_Density = mt.TEC_Density;
            _mt.TEC_Division = mt.TEC_Division;
            _mt.TEC_SEFI_Code = mt.TEC_SEFI_Code;
            _mt.TEC_Remark = mt.TEC_Remark;
            _mt.TEC_MTO_Code_SEFI_Code = mt.TEC_MTO_Code_SEFI_Code;
            _mt.TEC_Color_Code = mt.TEC_Color_Code;
            _mt.TEC_Top_Sefi = mt.TEC_Top_Sefi;
            _mt.TEC_Base_Unit_Of_Measure = mt.TEC_Base_Unit_Of_Measure;
            _mt.TEC_SAP_IP_Group = mt.TEC_SAP_IP_Group;
            _mt.TEC_Inspection_Setup = mt.TEC_Inspection_Setup;
            _mt.TEC_Insp_Type = mt.TEC_Insp_Type;
            _mt.TEC_Material_Group = mt.TEC_Material_Group;
            _mt.TEC_Unit_Of_Measure_X = mt.TEC_Unit_Of_Measure_X;
            _mt.TEC_Unit_Of_Measure_AUN = mt.TEC_Unit_Of_Measure_AUN;
            _mt.TEC_Unit_Of_Measure_Y = mt.TEC_Unit_Of_Measure_Y;
            _mt.TEC_Unit_Of_Measure_Bun = mt.TEC_Unit_Of_Measure_Bun;
            _mt.TEC_Gross_Weight = mt.TEC_Gross_Weight;
            _mt.TEC_Weight_Unit = mt.TEC_Weight_Unit;
            _mt.TEC_GenItemCatGroup = mt.TEC_GenItemCatGroup;
            _mt.TEC_Subline = mt.TEC_Subline;
            _mt.TEC_Pack_Size = mt.TEC_Pack_Size;
            _mt.PLA_FINI_Material_Code = mt.PLA_FINI_Material_Code;
            _mt.PLA_Plant = mt.PLA_Plant;
            _mt.PLA_Storage_Location = mt.PLA_Storage_Location;
            _mt.PLA_ICD = mt.PLA_ICD;
            _mt.PLA_IsPurchase = mt.PLA_IsPurchase;
            _mt.PLA_MRP_Controller = mt.PLA_MRP_Controller;
            _mt.PLA_MRP_Type = mt.PLA_MRP_Type;
            _mt.PLA_Lot_Size = mt.PLA_Lot_Size;
            _mt.PLA_Purchasing_Group = mt.PLA_Purchasing_Group;
            _mt.PLA_Strategy_Group = mt.PLA_Strategy_Group;
            _mt.PLA_Procurement_Type = mt.PLA_Procurement_Type;
            _mt.PLA_Special_Procurement = mt.PLA_Special_Procurement;
            _mt.PLA_Prod_Stor_Loc = mt.PLA_Prod_Stor_Loc;
            _mt.PLA_Storage_Loc_For_EP = mt.PLA_Storage_Loc_For_EP;
            _mt.PLA_MRP_Group = mt.PLA_MRP_Group;
            _mt.PLA_Fix_Lot_Size = mt.PLA_Fix_Lot_Size;
            _mt.PLA_Overdely_Tol = mt.PLA_Overdely_Tol;
            _mt.PLA_Unlimited = mt.PLA_Unlimited;
            _mt.PLA_Minimum_Lot_Size = mt.PLA_Minimum_Lot_Size;
            _mt.PLA_Maximum_Lot_Size = mt.PLA_Maximum_Lot_Size;
            _mt.PLA_Rounding_Value = mt.PLA_Rounding_Value;
            _mt.PLA_In_house_Production_Time = mt.PLA_In_house_Production_Time;
            _mt.PLA_Planned_Delivery_Time = mt.PLA_Planned_Delivery_Time;
            _mt.PLA_GR_Processing_Time = mt.PLA_GR_Processing_Time;
            _mt.PLA_Safety_Time_Act_Cov = mt.PLA_Safety_Time_Act_Cov;
            _mt.PLA_Safty_Stock = mt.PLA_Safty_Stock;
            _mt.PLA_Safety_Time_Ind = mt.PLA_Safety_Time_Ind;
            _mt.PLA_Stock_Det_Grp = mt.PLA_Stock_Det_Grp;
            _mt.PLA_With_Qty_Struture = mt.PLA_With_Qty_Struture;
            _mt.PLA_Valuation_Class = mt.PLA_Valuation_Class;
            _mt.PLA_Forecast_Model = mt.PLA_Forecast_Model;
            _mt.PLA_Plant_Sp_Matl_Status = mt.PLA_Plant_Sp_Matl_Status;
            _mt.PLA_Do_Not_Cost = mt.PLA_Do_Not_Cost;
            _mt.PLA_Is_MTS_MTO = mt.PLA_Is_MTS_MTO;
            _mt.PLA_ABC_Indicator = mt.PLA_ABC_Indicator;
            _mt.PLA_Backflush = mt.PLA_Backflush;
            _mt.PLA_Individual_Coll = mt.PLA_Individual_Coll;
            _mt.PLA_Planning_Material = mt.PLA_Planning_Material;
            _mt.PLA_Planning_Plant = mt.PLA_Planning_Plant;
            _mt.PLA_Selection_Method = mt.PLA_Selection_Method;
            _mt.PLA_Availability_Check = mt.PLA_Availability_Check;
            _mt.PLA_SchedMargin_Key = mt.PLA_SchedMargin_Key;
            _mt.PLA_Period_Indicator = mt.PLA_Period_Indicator;
            _mt.PLA_Deletion_Flag = mt.PLA_Deletion_Flag;
            _mt.PLA_Configurable_Material = mt.PLA_Configurable_Material;
            _mt.PLA_Consumption_Mode = mt.PLA_Consumption_Mode;
            _mt.PLA_Bwd_Comsumption = mt.PLA_Bwd_Comsumption;
            _mt.PLA_Fwd_Comsumption = mt.PLA_Fwd_Comsumption;
            _mt.PLA_Plng_Conv_Factor = mt.PLA_Plng_Conv_Factor;
            _mt.PLA_Planning_Matl_BUnit = mt.PLA_Planning_Matl_BUnit;
            _mt.FIN_Valuation_Class = mt.FIN_Valuation_Class;
            _mt.FIN_Material_Origin = mt.FIN_Material_Origin;
            _mt.FIN_Costing_Lot_Size = mt.FIN_Costing_Lot_Size;
            _mt.FIN_Price_Control = mt.FIN_Price_Control;
            _mt.FIN_Price_Unit = mt.FIN_Price_Unit;
            _mt.FIN_Origin_Group = mt.FIN_Origin_Group;
            _mt.FIN_Profit_Center = mt.FIN_Profit_Center;
            _mt.FIN_Currency = mt.FIN_Currency;
            _mt.FIN_Variance_Key = mt.FIN_Variance_Key;
            _mt.LOG_Coefficient_Of_Logistic = mt.LOG_Coefficient_Of_Logistic;
            _mt.LOG_LoadingGrp = mt.LOG_LoadingGrp;
            _mt.LOG_Storage_Location = mt.LOG_Storage_Location;
            _mt.LOG_Weight_Unit = mt.LOG_Weight_Unit;
            _mt.LOG_Net_Weight = mt.LOG_Net_Weight;
            _mt.LOG_Gross_Weight = mt.LOG_Gross_Weight;
            _mt.LOG_Volume_Unit = mt.LOG_Volume_Unit;
            _mt.LOG_Volume = mt.LOG_Volume;
            _mt.LOG_Trans_Grp = mt.LOG_Trans_Grp;
            _mt.LOG_Plant = mt.LOG_Plant;
            _mt.LOG_Unit_Of_Measure_X = mt.LOG_Unit_Of_Measure_X;
            _mt.LOG_Unit_Of_Measure_X_1 = mt.LOG_Unit_Of_Measure_X_1;
            _mt.LOG_Unit_Of_Measure_X_2 = mt.LOG_Unit_Of_Measure_X_2;
            _mt.LOG_Unit_Of_Measure_Y_1 = mt.LOG_Unit_Of_Measure_Y_1;           
            _mt.LOG_Unit_Of_Measure_AUN = mt.LOG_Unit_Of_Measure_AUN;
            _mt.LOG_Unit_Of_Measure_AUN_1 = mt.LOG_Unit_Of_Measure_AUN_1;
            _mt.LOG_Unit_Of_Measure_AUN_2 = mt.LOG_Unit_Of_Measure_AUN_2;
            _mt.LOG_Unit_Of_Measure_Y = mt.LOG_Unit_Of_Measure_Y;
            _mt.LOG_Unit_Of_Measure_Bun = mt.LOG_Unit_Of_Measure_Bun;
            _mt.LOG_Unit_Of_Measure_Bun_1 = mt.LOG_Unit_Of_Measure_Bun_1;
            _mt.LOG_Unit_Of_Measure_Bun_2 = mt.LOG_Unit_Of_Measure_Bun_2;
            _mt.LOG_Quantity_per_box = mt.LOG_Quantity_per_box;
            _mt.CA_Tax_Data = mt.CA_Tax_Data;
            _mt.CA_Gen_Item_Cat_Grp = mt.CA_Gen_Item_Cat_Grp;
            _mt.CA_Item_Category_Group = mt.CA_Item_Category_Group;
            _mt.CA_Acct_Assignment_Grp = mt.CA_Acct_Assignment_Grp;
            _mt.CA_Cash_Discount = mt.CA_Cash_Discount;
            _mt.CA_Dchain_Spec_Status = mt.CA_Dchain_Spec_Status;
            _mt.CA_Easy_Order_Entry = mt.CA_Easy_Order_Entry;
            _mt.PUR_Description_EN_1 = mt.PUR_Description_EN_1;
            _mt.PUR_Description_EN_2 = mt.PUR_Description_EN_2;
            _mt.PUR_Description_EN_3 = mt.PUR_Description_EN_3;
            _mt.PUR_Description_EN_4 = mt.PUR_Description_EN_4;
            _mt.PUR_Description_ZH_1 = mt.PUR_Description_ZH_1;
            _mt.PUR_Description_ZH_2 = mt.PUR_Description_ZH_2;
            _mt.PUR_Description_ZH_3 = mt.PUR_Description_ZH_3;
            _mt.PUR_Description_ZH_4 = mt.PUR_Description_ZH_4;
            _mt.PUR_Material_Group = mt.PUR_Material_Group;
            _mt.PUR_Lab_Office = mt.PUR_Lab_Office;
            _mt.PUR_GenItemCatGroup = mt.PUR_GenItemCatGroup;
            _mt.PUR_Base_Unit_Of_Measure = mt.PUR_Base_Unit_Of_Measure;
            _mt.PUR_Division = mt.PUR_Division;
            _mt.PUR_Purchasing_Group = mt.PUR_Purchasing_Group;
            _mt.PUR_Source_List = mt.PUR_Source_List;
            _mt.PUR_Order_Unit = mt.PUR_Order_Unit;
            _mt.PUR_Gross_Weight = mt.PUR_Gross_Weight;
            _mt.PUR_Net_Weight = mt.PUR_Net_Weight;
            _mt.PUR_Weight_Unit = mt.PUR_Weight_Unit;
            _mt.PUR_Volume = mt.PUR_Volume;
            _mt.PUR_Volume_Unit = mt.PUR_Volume_Unit;
            _mt.PUR_Classification = mt.PUR_Classification;
            _mt.PUR_Shape = mt.PUR_Shape;
            _mt.PUR_Top_Diameter = mt.PUR_Top_Diameter;
            _mt.PUR_Bottom_Diameter = mt.PUR_Bottom_Diameter;
            _mt.PUR_Height = mt.PUR_Height;
            _mt.PUR_Length = mt.PUR_Length;
            _mt.PUR_Width = mt.PUR_Width;
            _mt.PUR_Internalcoating = mt.PUR_Internalcoating;
            _mt.PUR_Decoration = mt.PUR_Decoration;
            _mt.PUR_Opening_Direction = mt.PUR_Opening_Direction;
            _mt.PUR_PO_Price = mt.PUR_PO_Price;
            _mt.PUR_Special_Procurement = mt.PUR_Special_Procurement;
            _mt.PUR_Pack_Size = mt.PUR_Pack_Size;
            _mt.PUR_Minimum_Lot_Size = mt.PUR_Minimum_Lot_Size;
            _mt.PUR_Fixed_Lot_Size = mt.PUR_Fixed_Lot_Size;
            _mt.PUR_Rounding_Value = mt.PUR_Rounding_Value;
            _mt.PUR_Maximum_Lot_Size = mt.PUR_Maximum_Lot_Size;
            _mt.PUR_Planned_Deliv_Time = mt.PUR_Planned_Deliv_Time;
            _mt.DMO_SAP_Code = mt.DMO_SAP_Code;
            _mt.DMO_Remarks = mt.DMO_Remarks;
            _mt.CA_Tax_classification = mt.CA_Tax_classification;
            _mt.CA_Tax_category = mt.CA_Tax_category;
            _mt.CA_Departure_country = mt.CA_Departure_country;
            _mt.CA_Minimun_delivery_quantity = mt.CA_Minimun_delivery_quantity;
            _mt.CA_Purchasing_Group = mt.CA_Purchasing_Group;
            _mt.CA_Availability_check = mt.CA_Availability_check;
            _mt.CA_Storage_Location = mt.CA_Storage_Location;
            _mt.LOG_Transportation_group = mt.LOG_Transportation_group;
            _mt.LOG_Warehouse_No = mt.LOG_Warehouse_No;
            _mt.LOG_Storage_Type = mt.LOG_Storage_Type;
            _mt.LOG_Stock_placement = mt.LOG_Stock_placement;
            _mt.LOG_Bulk_storage = mt.LOG_Bulk_storage;
            _mt.LOG_Storage_Bin = mt.LOG_Storage_Bin;
            _mt.LOG_Storage_Section_Ind = mt.LOG_Storage_Section_Ind;
            _mt.LOG_LE_quantity1 = mt.LOG_LE_quantity1;
            _mt.LOG_LE_quantity2 = mt.LOG_LE_quantity2;
            _mt.LOG_LE_quantity3 = mt.LOG_LE_quantity3;
            _mt.LOG_SUT1 = mt.LOG_SUT1;
            _mt.LOG_SUT2 = mt.LOG_SUT2;
            _mt.LOG_SUT3 = mt.LOG_SUT3;
            _mt.LOG_Maximum_bin_quantity = mt.LOG_Maximum_bin_quantity;
            _mt.LOG_Minimum_bin_quantity = mt.LOG_Minimum_bin_quantity;
            _mt.LOG_Control_quantity = mt.LOG_Control_quantity;
            _mt.LOG_Replenishment_qty = mt.LOG_Replenishment_qty;
            _mt.LOG_Unit_Of_Measure_X_D_1 = mt.LOG_Unit_Of_Measure_X_D_1;
            _mt.LOG_Unit_Of_Measure_AUN_A_1 = mt.LOG_Unit_Of_Measure_AUN_A_1;
            _mt.LOG_Unit_Of_Measure_Y_N_1 = mt.LOG_Unit_Of_Measure_Y_N_1;
            _mt.LOG_Unit_Of_Measure_Bun_B_1 = mt.LOG_Unit_Of_Measure_Bun_B_1;
            _mt.LOG_Stock_removal = mt.LOG_Stock_removal;
            _mt.MKT_Gross_Weight = mt.MKT_Gross_Weight;
            _mt.MKT_Length = mt.MKT_Length;
            _mt.MKT_Width = mt.MKT_Width;
            _mt.MKT_Height = mt.MKT_Height;
            _mt.MKT_Unit_of_Dimension = mt.MKT_Unit_of_Dimension;
            _mt.MKT_Volume = mt.MKT_Volume;
            _mt.MKT_Net_Weight = mt.MKT_Net_Weight;
            _mt.MKT_Material_Attributes_1 = mt.MKT_Material_Attributes_1;
            _mt.MKT_Material_Attributes_2 = mt.MKT_Material_Attributes_2;
            _mt.MKT_Material_Attributes_3 = mt.MKT_Material_Attributes_3;
            _mt.MKT_Dchain_spec_status = mt.MKT_Dchain_spec_status;
            _mt.MKT_Color_Code = mt.MKT_Color_Code;
            _mt.MKT_Pack_Size = mt.MKT_Pack_Size;
            _mt.MKT_Item_category_group = mt.MKT_Item_category_group;
            _mt.LOG_Total_Shelf_life = mt.LOG_Total_Shelf_life;
            _mt.LOG_Period_ind_For_SLED = mt.LOG_Period_ind_For_SLED;
            _mt.LOG_Min_Rem_Shelf_Life = mt.LOG_Min_Rem_Shelf_Life;

            return _mt;
        }
        #endregion

        #region 方法 Sheets 重新编号
        //private void RefreshSheetNo()
        //{
        //    for (int i = 0; i < Sheets.Items.Count; i++ )
        //    {
        //        string[] noSplit = Sheets.Items[i].Text.Split("-");
        //    }
        //}
        #endregion

        #region 方法 InitShowHide(初始化部门)
        private void InitShowHide()
        {
            PanelSheets.Visible = Status.IsMultiple;
            SetVisible(TaskAPI.IsAllowSubmit 
                    && Status.IsMultiple && Status.IsMultipleEditable, Add, Delete);

            #region 发起步骤
            if (TaskAPI.IsInitiate)
            {
                if (Materials.Count == 0)
                {
                    Materials.Clear();
                    Sheets.Items.Add(new ListItem("1", "1"));
                    mdc_Material mm = new mdc_Material();
                    mm.SheetStep = TaskAPI.Step;
                    mm.Status = 1;
                    mm.Step = TaskAPI.Step;
                    Materials.Add(mm);
                }
                else
                {
                    Sheets.Items.Clear();
                    for (int i = 0; i < Materials.Count; i++)
                    {
                        Sheets.Items.Add(new ListItem((i + 1).ToString(), (i + 1).ToString()));
                    }
                }
            }
            #endregion

            #region 各审批环节
            else
            {
                Materials = (List<mdc_Material>)(mdc_Material.process == TaskAPI.Process & mdc_Material.incident == TaskAPI.Incident
                                               & mdc_Material.keyword == Status.KeyWord & mdc_Material.status == 1).List<mdc_Material>(new Region(0, 1000), mdc_Material.sheetNo.Asc);
                Distribute(SheetIndex);

                Sheets.Items.Clear();
                for (int i = 0; i < Materials.Count; i++)
                {
                    if (TaskAPI.Process == "NPL Master Data Application" || TaskAPI.Process == "MTO Template Data Collection")
                    {
                        string SheetName = "";
                        SheetName = (Materials[i].SheetStep.IndexOf("Planning") >= 0 && Materials[i].SheetStep.Length > 2 ? (i + 1).ToString() + "." + Materials[i].SheetStep.Substring(0, 2) + "<" + Materials[i].SheetFrom + ">" : Materials[i].SheetNo);
                        Sheets.Items.Add(new ListItem(SheetName, (i + 1).ToString()));
                    }
                    else
                        Sheets.Items.Add(new ListItem((i + 1).ToString(), (i + 1).ToString()));
                }
            }

            SheetsCount.Text = Sheets.Items.Count.ToString();
            #endregion
        }
        #endregion
    }
}
